//
//  CCYMainTabBar.swift
//  03.微博
//
//  Created by 蔡承毅 on 16/9/17.
//  Copyright © 2016年 IOS. All rights reserved.
//

import UIKit

class CCYMainTabBar: UITabBar {
    
    var composeClosure:(() -> ())?
    
    
    lazy var composeButton: UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: .normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: .highlighted)
        
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), for: .normal)
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: .highlighted)
        
        button.addTarget(self, action: #selector(composeDidClick), for: .touchUpInside)
        
        self.addSubview(button)
        
        return button
    }()
    
    func composeDidClick() {
        composeClosure?()
    }
    
    // 按钮数量
    let count = 5
    
    override func layoutSubviews() {
        super.layoutSubviews()
        // 宽度
        let width = self.frame.width / CGFloat(count)
        let height = self.frame.height
        
        // 判断子类是否UITabBarButton
        var index = 0
        for subview in self.subviews {
            if subview.isKind(of: NSClassFromString("UITabBarButton")!) {
                let x = width * CGFloat(index)
                let y:CGFloat = 0
                
                subview.frame = CGRect(x: x, y: y, width: width, height: height)
                
                index += 1
                
                if index == 2 {
                    index += 1
                }
            }
        }
        
        
        // 修改frame
        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: height)
    }
}
